import EventCenter from "../../TFrame/script/helper/EventCenter";
import EventNames from "../EventNames";
import Util from "../../../StartAb/Util";

export interface TurnRewardItem {
    coin: number,
    degree: number
}

/**
 * 每天可3次转盘，第一次免费，第二三次广告
 */
export default class TurnTableSystem {

    static lastSpinTimeStamp = 0;

    static keyLastSpinTimeStamp = "keyLastSpinTimeStamp";

    //当天已经转了的天数
    static keyCurrentTurndTiems = 'keyCurrentTurndTiems';
    static currentTurnedTimes = 0;
    static maxTimesPerDay = 3;

    //转盘奖项
    static rewardArray = [30, 100, 50, 100, 80, 100, 150, 200];

    static load() {
        this.lastSpinTimeStamp = parseInt(cc.sys.localStorage.getItem(this.keyLastSpinTimeStamp)) || 0
        if (Util.getDayDiff(new Date(this.lastSpinTimeStamp), new Date()) > 0) {
            this.currentTurnedTimes = 0;//如果是新的一天，直接置0
        } else {
            this.currentTurnedTimes = parseInt(cc.sys.localStorage.getItem(this.keyCurrentTurndTiems)) || 0
        }
    }

    static persist() {
        cc.sys.localStorage.setItem(this.keyLastSpinTimeStamp, this.lastSpinTimeStamp);
        cc.sys.localStorage.setItem(this.keyCurrentTurndTiems, this.currentTurnedTimes);
    }

    static generateRewardItem(): TurnRewardItem {
        let rd = Util.random(0, this.rewardArray.length - 1);
        cc.log(`中奖区域:${rd}，中奖金额:${this.rewardArray[rd] * this.currentTurnedTimes}`)
        return { coin: this.rewardArray[rd] * this.currentTurnedTimes, degree: 45 * rd }
    }

    static spin() {
        this.currentTurnedTimes += 1;
        this.lastSpinTimeStamp = (new Date()).getTime();
        this.persist();
        return this.generateRewardItem();
    }

    static itsANewDay() {
        this.currentTurnedTimes = 0;
        this.persist();
        EventCenter.emit(EventNames.NewDaysEvent);
        console.log("******TurnTableSystem itsANewDay**********")
    }

    //如果状态还是满的，就不用状态检测了
    static isFullBlood() {
        if (this.lastSpinTimeStamp == 0)
            return true;
        if (this.currentTurnedTimes == 0)
            return true;
        return false
    }

    static isNoBlood() {
        return this.currentTurnedTimes >= this.maxTimesPerDay;
    }

}
